package com.it.latedata;

import com.it.pojo.Event;
import com.it.pojo.UrlVisitBean;
import com.it.windows.WindowAggAndProcessTest;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * 使用侧输出流的方式处理延迟数据.
 *
 * @author code1997
 */
public class LateDataTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        executionEnvironment.setParallelism(1);
        executionEnvironment.getConfig().setAutoWatermarkInterval(100);
        SingleOutputStreamOperator<Event> socketStream = executionEnvironment.socketTextStream("hadoop02", 7777).map(new MapFunction<String, Event>() {
            @Override
            public Event map(String value) throws Exception {
                String[] data = value.split(",");
                return new Event(data[0].trim(), data[1].trim(), Long.valueOf(data[2].trim()));
            }
        }).assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ofSeconds(2L)).withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
            @Override
            public long extractTimestamp(Event element, long recordTimestamp) {
                return element.timestamp;
            }
        }));
        socketStream.print("input");
        //定义输出标签：注意泛型擦除
        OutputTag<Event> lateTag = new OutputTag<Event>("lateStream") {
        };

        SingleOutputStreamOperator<UrlVisitBean> result = socketStream.keyBy(data -> data.url).window(TumblingEventTimeWindows.of(Time.seconds(10L)))
                //延迟1秒+2秒watermark=3s延迟,如果小于这个延迟时间，那么数据就会放到result里面，但是如果延迟超过这个时间那么就会输出到late的流中.
                .allowedLateness(Time.seconds(1))
                .sideOutputLateData(lateTag)
                .aggregate(new WindowAggAndProcessTest.MyAggFunction(), new WindowAggAndProcessTest.MyProcessFunction());
        result.print("result");
        //获取侧输出流的结果：只要前一个窗口没有关闭，一旦来属于其中的数据，那么就会触发一次计算.
        result.getSideOutput(lateTag).print("late");
        executionEnvironment.execute();
    }
}
